--
-- @Author:      feilong
-- @DateTime:    2019-17-30 23:05:48
-- @Description: 客户端消息的处理

local skynet = require "skynet"
-- local config = require "configquery"
-- local crypt = require "skynet.crypt"
local User = import("User")

local ClientHandler = class("ClientHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function ClientHandler:ctor(message_dispatch, node_message)
    self.message_dispatch = message_dispatch    
    self.node_message = node_message


	self:register()
end

--注册本服务里的消息
function ClientHandler:register()
	self.message_dispatch:registerSelf("enterHallReq", handler(self,self.enterHallReq))
	self.message_dispatch:registerSelf("matchReq", handler(self,self.matchReq))


end



function ClientHandler:start()
	self.user_manager = global.user_manager
	self.table_manager = global.table_manager
end




---------------------------------------------------------
-- CMD
---------------------------------------------------------


--用户进入
function ClientHandler:enterHallReq(data)
	print("___enterHallReq__",data)
	local user_id = data.user_id
	local user_info = self.node_message:callDbProxy("select", user_id, "user", {{user_id=user_id}})

	local user = User.new(data, user_info)
	self.user_manager:addUser(user)
    --返回给客户端
	local res = {
		enterHallRes={
			error_code=0,
			error_msg="进入成功",
			user = user:getClientInfo(),
		}
	}
	return res
end

--分配游戏
function ClientHandler:matchReq(data)
    print("___matchReq_msg_data__",data)
    local user_id = data.user_id
    local room_id = data.room_id

    --已有table_id直接进入游戏，没有则分配table_id
    self.table_manager:assignTable(room_id, user_id)

    --进入结果
    local enter_result = {
        error_code = 0,
    }
    return {enterGameRes = enter_result}
end








return ClientHandler